public class LeetCode876 {
    public ListNode middleNode(ListNode head) {
        ListNode lowNode = head;
        ListNode fastNode = head.next;

        while (lowNode != null && fastNode != null) {

            if (fastNode.next == null || fastNode.next.next == null) {
                return lowNode.next;
            }


            lowNode = lowNode.next;
            fastNode = fastNode.next.next;
        }
        return lowNode;
    }

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}